//Bayesian RBC with habit formation and 5 shocks El Salvador

var y       ${y}$           (long_name='output')
    x       ${x}$           (long_name='investment')
    c       ${c}$           (long_name='consumption')
    w       ${w}$           (long_name='real wages')
    R       ${B}$           (long_name='interest rate')
    k       ${k}$           (long_name='capital stock')
    d       ${d}$           (long_name='debt')
    r       ${r}$           (long_name='capital rent')
    l       ${l}$           (long_name='labor')
    la      ${la}$          (long_name='lambda')
    tb      ${tb}$          (long_name='trade balance')
    a       ${a}$           (long_name='TFP')
    v       ${v}$           (long_name='preference')
    g       ${g}$           (long_name='government spending')
    rer     ${rer}$         (long_name='real exchange rate')
    yw      ${yw}$               (long_name='world output')
    ;

varexo e_a    ${\epsilon_a}$       (long_name='TFP shock')
       e_v    ${\epsilon_v}$       (long_name='Preference shock')
       e_g    ${\epsilon_g}$       (long_name='Government spending shock')
       e_rer  ${\epsilon_rer}$     (long_name='Real exchange rate shock')
       e_yw   ${\epsilon_yw}$      (long_name='foreign sector shock')
      
    ;

parameters alp     ${\alpha}$        (long_name='capital share')
    bet            ${\beta}$         (long_name='discount factor')
    delt           ${\delta}$        (long_name='depreciation rate')
    sig            ${\sigma}$        (long_name='risk aversion')
    phi1           ${\phi}$          (long_name='marginal disutility of labor supply')
    phi2           ${\phi_2}$        (long_name='capital adjustment cost')
    psi1           ${\psi}$          (long_name='country premium')
    hf             ${\h}$            (long_name='habit formation')
    rhoa           ${\rho_a}$        (long_name='autocorrelation TPF')
    rhov           ${\rho_v}$        (long_name='autocorrelation preference')
    rhog           ${\rho_g}$        (long_name='autocorrelation government spending')
    rhorer         ${\rho_rer}$      (long_name='autocorrelation real exchange rate')
    rhoyw          ${\rho_yw}$       (long_name='autocorrelation foreign sector')
    ;

alp = 0.33;
bet = 0.95;
delt = 0.1;
sig = 2;
phi1 = 1.5;
phi2 = 5.6;
psi1 = 1.4;
hf = 0.5;
rhoa = 0.7;
rhov = 0.5;
rhog = 0.5;
rhorer = 0.3;
rhoyw = 0.55;

model(linear);
#rss = 1/bet + delt - 1;
#wss = (1-alp)*(alp/rss)^(alp/(1-alp));
#A1 = (((1-hf*bet)*wss^(1+phi1))/(1-alp)^phi1)^(1/sig);
#yss = (A1*2*rss/((1-hf)*(rss-2*delt*alp)))^(sig/(sig+phi1));
#css = (1/(1-hf))*A1*yss^(-phi1/sig);
#iss = delt*alp*yss/rss;
#tbss = 0.5*yss - 1;
//1-Lagrangian household
la = (1/(1-hf*bet))*(v-hf*bet*v(+1)+(sig/(1-hf))*(hf*bet*(c(+1)-hf*c)-(c-hf*c(-1))));
//2-Labor supply
phi1*l = la + w - v;
//3-Investment demand
x = k + bet*(x(+1)-k(+1)) + (1/(phi2*iss))*(la(+1)-la+(1+bet*delt-bet)*r(+1));
//4-Consumption
css*c = yss*y - iss*x - g - tbss*tb;
//5-Debt equation
yss*d = 0.5*yss*d(+1) + (0.5*yss - 1)*tb + g - 0.25*yss*R;
//6-Production function
y = a + alp*k + (1-alp)*l;
//7-Labor demand
w = y - l;
//8-Demand for capital
r = y - k;
//9-Trade balance equation
tb = (2/(yss*(yss-2)))*(yw-y+rer);
//10-Interest rate 
R = psi1*(d(+1));
//11-Capital supply
k = (1-delt)*k(-1) + delt*x;
//12-Productivity shock
a = rhoa*a(-1) + e_a; 
//13-Preference shock
v = rhov*v(-1) + e_v; 
//14-Government spending shock
g = rhog*g(-1) + e_g;
//15-Real exchange rate shock
rer = rhorer*rer(-1) + e_rer;
//16-Foreign output shock
yw = rhoyw*yw(-1) + e_yw;
end;

estimated_params;
stderr e_a, inv_gamma_pdf, 0.01, inf;
stderr e_v, inv_gamma_pdf, 0.01, inf;
stderr e_g, inv_gamma_pdf, 0.01, inf;
stderr e_rer, inv_gamma_pdf, 0.01, inf;
stderr e_yw, inv_gamma_pdf, 0.01, inf;
alp, normal_pdf, 0.33, 0.02;
bet, uniform_pdf, , , 0.9, 0.99;
delt, normal_pdf, 0.1, 0.001;
sig, normal_pdf, 2, 0.1;
phi1, normal_pdf, 1.5, 0.1;
phi2, normal_pdf, 5.6, 0.5;
psi1, normal_pdf, 1.4, 0.5;
hf, beta_pdf, 0.5, 0.2;
rhoa, beta_pdf, 0.7, 0.2;
rhov, beta_pdf, 0.5, 0.2;
rhog, beta_pdf, 0.5, 0.2;
rhorer, uniform_pdf, , , -1, 1;
rhoyw, beta_pdf, 0.55, 0.1;
end;

varobs y c x tb yw;
estimation(datafile='slv.xlsx', nobs=60, mh_replic=20000, mh_jscale=0.5, consider_all_endogenous, smoother, nodiagnostic, moments_varendo, contemporaneous_correlation, bayesian_irf);
shock_decomposition y;

